package com.jinwei.qunguanjia.web.group.controller;

import com.jinwei.qunguanjia.common.core.domain.Response;
import com.jinwei.qunguanjia.common.core.page.PageDataInfo;
import com.jinwei.qunguanjia.common.utils.*;
import com.jinwei.qunguanjia.user.dto.GroupDTO;
import com.jinwei.qunguanjia.user.service.*;
import com.jinwei.qunguanjia.web.group.vo.GroupVO;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 群管理相关接口
 *
 * @author yuanzs
 */
@RestController
@RequestMapping("/group")
public class GroupController {

    @Resource
    private GroupService groupService;

    /**
     * 获取群分页列表
     * @param groupVO
     */
    @GetMapping("/listPage")
    public PageDataInfo<GroupVO> list(GroupVO groupVO) {
        GroupDTO groupDTO = new GroupDTO();
        BeanUtils.copyProperties(groupVO, groupDTO);
        PageUtils.startPage();
        PageDataUtil<GroupDTO, GroupVO> pageDataUtil = new PageDataUtil<>();
        return pageDataUtil.transform(groupService.listPage(groupDTO), GroupVO.class);
    }

    /**
     * 获取群详细信息
     * @param groupId 群id
     * @return 群详细信息
     */
    @GetMapping(value = "/{groupId}")
    public Response<GroupVO> getInfo(@PathVariable String groupId) {
        GroupDTO groupDTO = groupService.get(groupId);
        GroupVO groupVO = new GroupVO();
        BeanUtils.copyProperties(groupDTO, groupVO);
        return Response.success(groupVO);
    }

    /**
     * 审核通过
     * @param groupVO 对象
     */
    @PutMapping("/auditAgree")
    public Response auditAgree(@Validated @RequestBody GroupVO groupVO) {
        groupService.auditAgree(groupVO.getGroupId());
        return Response.success();
    }

    /**
     * 审核拒绝
     * @param groupVO 对象
     */
    @PutMapping("/auditRefuse")
    public Response auditRefuse(@Validated @RequestBody GroupVO groupVO) {
        groupService.auditRefuse(groupVO.getGroupId(), groupVO.getReason());
        return Response.success();
    }
    
}
